<template>
  <div class="hyydxq">
    <van-cell class="topCeiling">
      <div class="flex-between topDate">
        <p :class="['line-text-out', !!params.startDate ? 'col-333333' : 'col-37B5B5']" @click="timeSelection(1)">
          {{ params.startDate || '开始时间' }}
        </p>
        <p class="col-999999">至</p>
        <p
          :class="['line-text-out', 'text-right', !!params.endDate ? 'col-333333' : 'col-37B5B5']"
          @click="timeSelection(2)"
        >
          {{ params.endDate || '结束时间' }}
        </p>
      </div>
      <van-popup v-model="showPicker1" position="bottom">
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          @confirm="onConfirmStartDate"
          @cancel="showPicker1 = false"
        />
      </van-popup>
    </van-cell>
    <!-- 预约信息 -->
    <van-pull-refresh style="margin-top: 55px" v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" :finished-text="list.length > 0 ? '没有更多了' : ''" @load="onLoad">
        <div v-if="list.length > 0">
          <van-cell
          v-for="(item, key) in list"
          :key="key"
          class="approvalItem content"
          @click="byPregnancyRecord(item.id)"
        >
          <div class="flex-between font-14 line-height-3">
            <div class="col-333333 font-bold-500 font-15 m-r-10">{{ item.verifyDate | dateFormat('YYYY-MM-DD') }}</div>
            <div class="col-999999 line-text-out text-right"></div>
          </div>
          <div class="flex-between font-13 line-height-3">
            <div class="col-666666 m-r-10">受孕成功数量（只）：{{ item.pregnancySuccessNum || '' }}</div>
          </div>
          <div class="flex-between font-13 line-height-3">
            <div class="col-666666 line-text-out text-right">创建人：{{ item.createdByName || '' }}</div>
          </div>
          <div class="flex-between font-13 line-height-3">
            <div class="col-666666 m-r-10 line-text-out">创建时间：{{ item.createTime || '' }}</div>
          </div>
        </van-cell>
        </div>
        <temporarily-data v-else />
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
//保养记录
import { getValue, setValue, clearValue} from '@/utils/auth'
import { Toast, Cell, CellGroup, Popup, Picker, DatetimePicker, PullRefresh, CheckboxGroup, List } from 'vant'
import { getHandle, postHandle } from '@/api'
import dayjs from 'dayjs'
import TemporarilyData from '@/components/temporarilyData'
export default {
  components: {
    TemporarilyData,
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
    [Popup.name]: Popup,
    [Picker.name]: Picker,
    [DatetimePicker.name]: DatetimePicker,
    [PullRefresh.name]: PullRefresh,
    [CheckboxGroup.name]: CheckboxGroup,
    [Toast.name]: Toast,
    [List.name]: List
  },
  data() {
    return {
      startDate: '',
      endDate: '',
      showPicker1: false,
      refreshing: false,
      currentDate: new Date(),
      list: [],
      loading: false,
      finished: false,
      params: {
        page: 1,
        perPage: 10,
        startDate: '',
        endDate: '',
        houseInfoId: getValue('fzmHourseId')
        // houseInfoName: '羔羊舍'
        // equipmentId: getValue('id')
      }
    }
  },
  watch: {},
  created() {},
  mounted() {
    this.$bridge.register('refresh', data => {
      this.refreshing = true
      this.params.page = 1
      clearValue('syObj')
      clearValue('earTag2')
      clearValue('fzmSex')
      this.onLoad()
    })
  },
  methods: {
    byPregnancyRecord(id) {
      setValue('syId', id)
      this.$bridge.call('detailsPregnancyRecord')
    },
    timeSelection(e) {
      this.showPicker1 = true
      this.flag = e
    },
    onConfirmStartDate(value) {
      let timestamp = dayjs(value).valueOf()
      if (this.flag == 1) {
        if (timestamp <= dayjs(this.params.endDate || '2031-12-31').valueOf()) {
          this.params.startDate = dayjs(value).format('YYYY-MM-DD')
        } else {
          Toast('不得晚于结束时间')
          return
        }
      } else {
        if (timestamp >= dayjs(this.params.startDate || '1987-1-1').valueOf()) {
          this.params.endDate = dayjs(value).format('YYYY-MM-DD')
        } else {
          Toast('不得早于开始时间')
          return
        }
      }
      if (!!this.params.startDate && !!this.params.endDate) {
        this.refreshing = true
        this.onLoad()
      }
      this.showPicker1 = false
    },
    // 卡片列表
    onRefresh() {
      this.params.page = 1
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    onSearch() {
      this.params.page = 1
      this.list = []
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    onLoad() {
      getHandle('/agri/api/yzbzh/pregnancyRecord/queryListPage', this.params).then(res => {
        if (this.refreshing) {
          this.list = []
          this.refreshing = false
        }
        this.list = this.list.concat(res.data.rows)
        this.loading = false

        if (this.list.length >= res.data.count) {
          this.finished = true
        } else {
          this.params.page = this.params.page + 1
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.topCeiling {
  position: fixed;
  top: 0;
  z-index: 1;
}
.topDate {
  width: 80%;
  margin: auto;
  display: flex;
  justify-content: space-between;
  > p {
    font-size: 15px;
    &:first-child {
      width: 40%;
    }
    &:last-child {
      width: 40%;
    }
  }
}
.van-field__control {
  border: 0 !important;
}
.content {
  //   border-bottom: 1px solid #eee;
  padding: 10px 15px;
}
</style>
